package com.qxsx.midi.activity.me;

import android.content.Intent;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.adapter.MusicAdapter;
import com.qxsx.midi.model.MusicData;
import com.qxsx.midi.utils.MediaUtils;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.callBackJsonArray;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Joney on 2015/9/21.
 * 音频列表
 */
public class ApplyMusicSelectActivity extends BaseActivity implements OnClickListener {

    public ListView mListView;
    private List<MusicData> lists;
    private MusicAdapter adapter;
    String srcPath = "";

    public static final String KEY_YINPIN_PATH = "KEY_YINPIN_PATH";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_select);
        setNavBtn(R.mipmap.back, 0);
        setTitle("音频列表");
        MediaScannerConnection.scanFile(this, new String[]{Environment
                .getExternalStorageDirectory().getPath()}, null, null);
        findById();
        inItData();

    }

    private void inItData() {


        lists = new ArrayList<>();
        lists = MediaUtils.getAudioList(this);
        adapter = new MusicAdapter(this, lists);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                srcPath = lists.get(position).getPath().toString();
                Intent intent = new Intent();
                intent.putExtra(KEY_YINPIN_PATH, srcPath);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void findById() {
        mListView = (ListView) findViewById(R.id.listView);


    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {

        switch (component) {
            case LEFT:
                finish();
                break;

        }

    }



    @Override
    public void onClick(View v) {

    }
}
